package com.houseman.workFlow.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.houseman.workFlow.bean.WorkFlowFolder;
import com.houseman.workFlow.bean.WorkFlowGraph;
import com.houseman.workFlow.dao.FolderDao;
import com.houseman.workFlow.dao.GraphDao;
import com.houseman.workFlow.dao.impl.FolderDaoImpl;
import com.houseman.workFlow.dao.impl.GraphDaoImpl;
import com.houseman.workFlow.service.FolderService;

public class FolderServiceImpl implements FolderService{
	
	//dao
	FolderDao fdao = new FolderDaoImpl();
	GraphDao gdao = new GraphDaoImpl();
	/**
	 * 获得文件夹集信息
	 * @return
	 * @throws Exception
	 */
	public List<WorkFlowFolder> queryFolders() throws Exception {
		return fdao.queryFolders();
	}
	
	/**
	 * 获得文件夹以及文件夹下的流程
	 * @return
	 * @throws Exception
	 */
	public List<WorkFlowFolder> queryFoldersAndGraphs() throws Exception {
		List<WorkFlowGraph> graphs = gdao.queryGraphsByFolder();
		List<WorkFlowFolder> folders =  fdao.queryFolders();
		
		for (int i = 0; i < folders.size(); i++) {
			List<WorkFlowGraph> ghs = new ArrayList<WorkFlowGraph>();
			for (int j = 0; j < graphs.size(); j++) {
				if(folders.get(i).getFolderId() == graphs.get(j).getFolderId()) {
					ghs.add(graphs.get(j));
				}
			}
			folders.get(i).setGraphs(ghs);
		}
		
		return folders;
	}
	
	/**
	 * 保存文件夹
	 * @param folder
	 * @throws Exception
	 */
	public void saveFolder(WorkFlowFolder folder) throws Exception {
		fdao.saveFolder(folder);
	}
}
